<template>
  <div>
    <SearchForm
        @search="onSearch"
        @reset="onReset"
        :search-list="searchList" :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-handle">
          <el-button type="primary" size="mini" icon="el-icon-plus" @click="onAdd">新增</el-button>
          <el-button type="success" size="mini" icon="el-icon-download" @click="onExport">导出数据</el-button>
        </div>
        <div class="table-box">
          <el-table
              size="large"
              stripe
              border
              v-loading="loading"
              max-height="500px"
              :data="tableData"
              row-key="id"
              element-loading-spinner="el-icon-loading"
              element-loading-background="rgba(255, 255, 255, 0.8)"
              @selection-change="onSelect"
          >
            <el-table-column prop="id" label="ID" width="60"/>
            <el-table-column prop="name" label="姓名" width="100"/>
            <el-table-column prop="id_card" label="身份证号" width="180"/>
            <el-table-column prop="phone" label="联系电话" width="130"/>
            <el-table-column prop="gender" label="性别" width="80"/>
            <el-table-column prop="marital_status" label="婚姻状况" width="100"/>
            <el-table-column prop="education" label="学历" width="100"/>
            <el-table-column prop="co_applicant" label="共同申领人" width="120"/>
            <el-table-column prop="relationship" label="关系" width="100"/>
            <el-table-column prop="work_unit" label="工作单位" width="200">
              <template slot-scope="scope">
                <el-tooltip v-if="scope.row.work_unit && scope.row.work_unit.length > 15" :content="scope.row.work_unit" placement="top">
                  <span>{{ scope.row.work_unit.substring(0, 15) }}...</span>
                </el-tooltip>
                <span v-else>{{ scope.row.work_unit || '-' }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="status" label="审核状态" width="100">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.status === 0" type="warning">待审核</el-tag>
                <el-tag v-else-if="scope.row.status === 1" type="success">审核通过</el-tag>
                <el-tag v-else type="danger">审核拒绝</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="create_time" label="创建时间" width="160"/>
            <el-table-column label="操作" fixed="right" width="200">
              <template slot-scope="scope">
                <el-button type="text" @click="onEdit(scope.row)">编辑</el-button>
                <el-button type="text" style="color: #F82222" @click="onDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
            background
            :current-page="pageNo"
            :page-sizes="pageSizes"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalSize"
            @size-change="changePageSize"
            @current-change="changePageNo"
        />
      </div>
    </div>
    <Add ref="addFormRef" @get-page="initData" />
  </div>
</template>
<script>

import crud from "@/mixins/crud";
import { http } from "@/api";
import Add from "./add";
import { exportToExcel } from "@/utils";

export default {
  name: "SettlementSubsidyIndex",
  components: {
    Add
  },
  mixins: [crud],
  data() {
    return {
      request: http.settlementSubsidy,
      searchList: [
        {
          label: '姓名',
          prop: 'name',
          name: 'input',
          placeholder: '请输入姓名'
        },
        {
          label: '身份证号',
          prop: 'id_card',
          name: 'input',
          placeholder: '请输入身份证号'
        },
        {
          label: '联系电话',
          prop: 'phone',
          name: 'input',
          placeholder: '请输入联系电话'
        },
        {
          label: '性别',
          prop: 'gender',
          name: 'select',
          options: [
            { value: '男', label: '男' },
            { value: '女', label: '女' }
          ]
        },
        {
          label: '婚姻状况',
          prop: 'marital_status',
          name: 'select',
          options: [
            { value: '未婚', label: '未婚' },
            { value: '已婚', label: '已婚' },
            { value: '离异', label: '离异' }
          ]
        },
        {
          label: '审核状态',
          prop: 'status',
          name: 'select',
          options: [
            { value: 0, label: '待审核' },
            { value: 1, label: '审核通过' },
            { value: 2, label: '审核拒绝' }
          ]
        }
      ],
      searchParams: {
        name: '',
        id_card: '',
        phone: '',
        gender: '',
        marital_status: '',
        status: ''
      },
      defaultParams: {
      },
      tableData: [],
      // 导出表头配置（包含add.vue中的所有字段）
      exportHeaders: [
        { prop: 'id', label: 'ID' },
        { prop: 'name', label: '姓名' },
        { prop: 'id_card', label: '身份证号码' },
        { prop: 'gender', label: '性别' },
        { prop: 'phone', label: '联系电话' },
        { prop: 'marital_status', label: '婚姻状况' },
        { prop: 'education', label: '学历' },
        { prop: 'household_location', label: '户籍所在地' },
        { prop: 'work_start_date', label: '参加工作时间' },
        { prop: 'co_applicant', label: '共同申领人' },
        { prop: 'relationship', label: '关系' },
        { prop: 'work_unit', label: '工作单位' },
        { prop: 'status', label: '审核状态' },
        { prop: 'reviewer', label: '审核人' },
        { prop: 'review_remark', label: '审核备注' },
        { prop: 'create_time', label: '创建时间' }
      ]
    }
  },
  methods: {
    // 导出数据
    async onExport() {
      try {
        this.$message.info('正在准备导出数据...')

        // 获取所有符合条件的数据（不分页）
        const exportParams = {
          ...this.defaultParams,
          pageNo: 1,
          pageSize: 99999 // 设置一个很大的值来获取所有数据
        }

        // 添加搜索条件
        for (const searchParamsKey in this.searchParams) {
          if (this.searchParams[searchParamsKey] !== '' && this.searchParams[searchParamsKey] !== null && this.searchParams[searchParamsKey] !== undefined) {
            if (searchParamsKey === 'timeValue') {
              if (this.searchParams[searchParamsKey] && this.searchParams[searchParamsKey].length) {
                exportParams['beginTime'] = this.searchParams.timeValue[0] || undefined
                exportParams['endTime'] = this.searchParams.timeValue[1] || undefined
              }
            } else {
              exportParams[searchParamsKey] = this.searchParams[searchParamsKey]
            }
          }
        }

        const response = await this.request.page(exportParams)
        const exportData = response.data.list || []

        if (exportData.length === 0) {
          this.$message.warning('没有符合条件的数据可导出')
          return
        }

        // 调用导出函数
        exportToExcel(exportData, this.exportHeaders, '安家补贴申请')

        this.$message.success(`成功导出 ${exportData.length} 条数据`)

      } catch (error) {
        console.error('导出失败:', error)
        this.$message.error('导出失败，请重试')
      }
    }
  }
}
</script>

<style scoped lang="scss">
.table-layout {
  margin-top: 20px;
}
.table-handle {
  margin-bottom: 20px;
}
.page-box {
  margin-top: 20px;
  text-align: right;
}
</style>
